const trigger = (el, type) => {
	const e = document.createEvent('HTMLEvents')
	e.initEvent(type, true, true)
	el.dispatchEvent(e)
}

export default {
	number: {
		bind: function(el) {
			const input = el.getElementsByTagName('input')[0]
			input.onkeyup = function(e) {
				if (input.value.length === 1) {
					input.value = input.value.replace(/[^0-9-]/g, '')
				} else {
					input.value = input.value.replace(/[^\d-]/g, '')
				}
				trigger(input, 'input')
			}
			input.onblur = function(e) {
				if (input.value.length === 1) {
					input.value = input.value.replace(/[^0-9-]/g, '')
				} else {
					input.value = input.value.replace(/[^\d-]/g, '')
				}
				trigger(input, 'input')
			}
		}
	},
	has: {
		//初始化指令
		inserted(el, binding) {
			let menuAuth = uni.getStorageSync('menuAuth');
			if (!menuAuth) {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			}
			if (Array.isArray(binding.value)) {
				let bindval = binding.value;
				let flag = false;
				bindval.forEach(v => {
					if (menuAuth.includes(v)) {
						flag = true;
					}
				})
				if (!flag) {
					el.parentNode && el.parentNode.removeChild(el);
				}
			} else {
				if (!menuAuth.includes(binding.value)) {
					el.parentNode && el.parentNode.removeChild(el);
				}
			}
		}
	}
}